package me.zfz.business.controller.impl;

import javax.servlet.http.HttpSession;

import me.zfz.base.bean.SmartResponse;
import me.zfz.base.constant.IConstant;
import me.zfz.business.bean.RequestPage;
import me.zfz.business.bean.UserInfo;
import me.zfz.business.bean.entity.TNOrg;
import me.zfz.business.constant.IWebConstant;
import me.zfz.business.controller.base.BaseController;
import me.zfz.base.plugins.OrgZTreeData;
import me.zfz.business.service.OPService;
import me.zfz.business.service.OrgService;
import me.zfz.base.tag.bean.DelBtn;
import me.zfz.base.tag.bean.EditBtn;
import me.zfz.base.tag.bean.PageParam;
import me.zfz.base.tag.bean.RefreshBtn;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import me.zfz.business.bean.UserSearchParam;

/**
 * 学院
 *
 * @author author
 */
@Controller
@RequestMapping("org")
public class OrgController extends BaseController {

    private static final String VIEW_DIR = IWebConstant.WEB_BASE_VIEW_DIR + "/org";

    @Autowired
    private OrgService orgServ;
    @Autowired
    private OPService opServ;

    @RequestMapping("list")
    public ModelAndView list(ModelAndView modelView) throws Exception {
        SmartResponse<Object> smartResp = opServ.getTreeDatas("org_mgr_tree_list");
        String uri = "org/list";
        addBtn = new EditBtn("add", "showPage/base_org_add", "org", "添加学院", "600");
        editBtn = new EditBtn("edit", "showPage/base_org_edit", "org", "修改学院", "600");
        delBtn = new DelBtn("org/delete.json", "确定要删除选中的学院吗？（注：如果该学院下面有子学院的话，会一起删除的哦~）", uri, null, null);
        refreshBtn = new RefreshBtn(uri, "org", null);

        ModelMap modelMap = modelView.getModelMap();
        modelMap.put("smartResp", smartResp);
        modelMap.put("addBtn", addBtn);
        modelMap.put("editBtn", editBtn);
        modelMap.put("delBtn", delBtn);
        modelMap.put("refreshBtn", refreshBtn);
        modelView.setViewName(VIEW_DIR + "/list");
        return modelView;
    }

    /**
     * 添加组织架构
     *
     * @param session
     * @param org
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public @ResponseBody
    SmartResponse<String> add(HttpSession session, TNOrg org) throws Exception {
        SmartResponse<String> smartResp = new SmartResponse<String>();
        if (null != org) {
            smartResp = orgServ.save(org);
            if (IConstant.OP_SUCCESS.equals(smartResp.getResult())) {
                UserInfo userInfo = super.getUserInfoFromSession(session);
                userInfo.getOrgIds().add(smartResp.getData());
            }
        }
        return smartResp;
    }

    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public @ResponseBody
    SmartResponse<String> edit(TNOrg org) throws Exception {
        SmartResponse<String> smartResp = new SmartResponse<String>();
        if (null != org) {
            smartResp = orgServ.update(org);
        }
        return smartResp;
    }

    /**
     * 删除
     *
     * @param session
     * @param id
     * @return
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public SmartResponse<String> delete(HttpSession session, String id) {
        SmartResponse<String> smartResp = orgServ.delete(id);
        if (IConstant.OP_SUCCESS.equals(smartResp.getResult())) {
            UserInfo userInfo = super.getUserInfoFromSession(session);
            userInfo.getOrgIds().remove(id);
        }
        return smartResp;
    }

    @RequestMapping("/tree")
    public @ResponseBody
    SmartResponse<OrgZTreeData> tree(HttpSession session, String treeType) throws Exception {
        return orgServ.getZTree(getUserInfoFromSession(session).getOrgIds(), treeType);
    }

    /**
     * 该用户拥有的角色列表
     *
     * @return
     * @throws Exception
     */
    @RequestMapping("/rolelist")
    public ModelAndView rolelist(UserSearchParam searchParam, ModelAndView modelView, RequestPage page) throws Exception {
        String uri = "org/rolelist";
        SmartResponse<Object> smartResp = opServ.getDatas("org_role_list", searchParam, page.getStartNum(), page.getPageSize());
        pageParam = new PageParam(uri, null, page.getPage(), page.getPageSize());
        uri = uri + "?id=" + searchParam.getId();
        addBtn = new EditBtn("add", "org/addRole?id=" + searchParam.getId(), "该学院中添加角色", "600");
        delBtn = new DelBtn("org/deleteRole?orgId=" + searchParam.getId(), "确定要从该学院中删除选中的角色吗？", uri, "#org-role-tab", null);
        refreshBtn = new RefreshBtn(uri, null, "#org-role-tab");

        ModelMap modelMap = modelView.getModelMap();
        modelMap.put("smartResp", smartResp);
        modelMap.put("addBtn", addBtn);
        modelMap.put("delBtn", delBtn);
        modelMap.put("refreshBtn", refreshBtn);
        modelMap.put("pageParam", pageParam);
        modelMap.put("searchParam", searchParam);
        modelView.setViewName(VIEW_DIR + "/rolelist");
        return modelView;
    }

}
